package com.itheima.web;

import com.itheima.util.CheckCodeUtil;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet("/CheckCodeServlet")
public class CheckCodeServlet extends HttpServlet {

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {


        //给注册页面返回一张验证码图片

        //获取response输出对象
        ServletOutputStream outputStream = response.getOutputStream();
        //使用CheckCode工具类获取验证码图片
        String s = CheckCodeUtil.outputVerifyImage(100, 50, outputStream, 4);//验证码的长度，宽度，输出流，长度

        //将s放入session中，用户后期验证码的比对
        HttpSession session = request.getSession();
        session.setAttribute("checkCode",s);
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }
}